importing a VCard in the address book , objective C [migrated]
Posted
by
user1044771
on Programmers
See other posts from Programmers
or by user1044771
Published on 2011-11-16T19:49:54Z
Indexed on
2011/11/17
2:04 UTC
Read the original article
Hit count: 1219
I am designing a QR code reader, and it needs to detect and import contact cards in vCard format.
is there a way to add the card data to the system Address Book directly, or do I need to parse the vCard myself and add each field individually?
I will be getting the VCArd in a NSString format
I tried the code below (from a different post) and didn't work
-(IBAction)saveContacts{
NSString *vCardString = @"vCardDataHere";
CFDataRef vCardData = (__bridge_retained CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(book, person, NULL);
CFRelease(person);
}
CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(book, NULL);
CFRelease(book);
}
I have searched a bit and fixed the code and here how it looks like it doesn t crash anymore but it doesn t save the VCard (NSString format) in the address book , any clues ?
© Programmers or respective owner